package com.gzu.finalwork.admin.controller;

import com.gzu.finalwork.admin.service.EmailSettingsService;
import com.gzu.finalwork.entity.admin.EmailSettings;
import com.gzu.finalwork.common.utils.AjaxResult;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 邮件设置控制器
 */
@RestController
@RequestMapping("/admin/email-settings")
public class EmailSettingsController {

    @Autowired
    private EmailSettingsService emailSettingsService;

    /**
     * 获取邮件设置
     */
    @GetMapping
    public Object getEmailSettings() {
        Map<String, Object> result = new HashMap<>();
        try {
            EmailSettings settings = emailSettingsService.getEmailSettings();
            result.put("success", true);
            result.put("data", settings);
            return AjaxResult.success(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "获取邮件设置失败：" + e.getMessage());
            return AjaxResult.error(500, result);
        }
    }

    /**
     * 更新邮件设置
     */
    @PutMapping
    public Object updateEmailSettings(@RequestBody EmailSettings emailSettings) {
        Map<String, Object> result = new HashMap<>();
        try {
            emailSettingsService.updateEmailSettings(emailSettings);
            result.put("success", true);
            result.put("message", "更新邮件设置成功");
            return AjaxResult.success(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "更新邮件设置失败：" + e.getMessage());
            return AjaxResult.error(500, result);
        }
    }

    /**
     * 测试邮箱连接
     */
    @PostMapping("/test")
    public Object testEmailConnection(@RequestBody EmailSettings emailSettings) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean isConnected = emailSettingsService.testEmailConnection(emailSettings);
            result.put("success", isConnected);
            result.put("message", isConnected ? "邮箱连接成功" : "邮箱连接失败");
            return AjaxResult.success(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "邮箱连接失败：" + e.getMessage());
            return AjaxResult.error(500, result);
        }
    }
} 